package com.graduate.cec.controller;


import com.graduate.cec.common.JsonResult;
import com.graduate.cec.pojo.dto.NationDetailDTO;
import com.graduate.cec.pojo.vo.NationDetailVO;
import com.graduate.cec.service.NationDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = "/nation-detail", produces = "application/json;charset=utf-8")
@Api(tags = "文档内容")
public class NationDetailController {
    @Resource
    private NationDetailService nationDetailService;



    /****************        增       ******************************************************************/
    @PostMapping("/insert-nation-detail")
    @ApiOperation("增加内容")
    public JsonResult<NationDetailVO> insertNationDetail(@RequestBody NationDetailDTO nationDetailDTO) {
        NationDetailVO nationDetailVO = nationDetailService.insertNationDetail(nationDetailDTO);
        return JsonResult.ok("插入成功", nationDetailVO);
    }


    //上传图片
    @PostMapping("/upload-picture")
    @ApiOperation("上传图片")
    public JsonResult<String> upload(MultipartFile picFile) throws Exception {
        String fileName = nationDetailService.uploadPictures(picFile);
        return JsonResult.ok("插入成功",fileName);

    }

    /****************        删       ******************************************************************/

    //删除上传的图片
    @GetMapping("/remove-picture")
    @ApiOperation("删除图片")
    public JsonResult<Void> remove(String pictureName) {
        nationDetailService.removePicture(pictureName);
        return JsonResult.ok("删除成功");
    }


    @DeleteMapping("/delete-by-keys/{ids}")
    @ApiOperation("通过主键删除内容")
    public JsonResult<Void> deleteByKeys(@PathVariable Long ids) {
        log.info("ids:{}", ids);
        nationDetailService.deleteByKeys(ids);
        return JsonResult.ok("删除成功");
    }


    /****************        改       ******************************************************************/
    @PostMapping("/update-by-key")
    @ApiOperation("通过主键修改内容")
    public JsonResult<Void> updateByKey(@RequestBody NationDetailDTO nationDetailDTO) {
        log.info("nationDetailDTO:{}", nationDetailDTO);
        nationDetailService.updateByKey(nationDetailDTO);
        return JsonResult.ok("修改成功");
    }


    /****************        查        ******************************************************************/
    /**
     * 根据查询条件查找列表
     * @param nationId 条件
     * @return JsonResult<JsonPage<NationDetailVO>>
     */
    @GetMapping("/get-list-by-example")
    @ApiOperation("通过一定条件(目录id)查找内容")
    public JsonResult<NationDetailVO> getListByExample(@RequestParam(value = "nationId") Long nationId){
        NationDetailVO listByExample = nationDetailService.getListByExample(nationId);
        return JsonResult.ok("查询成功",listByExample);
    }

}
